<!DOCTYPE html>
<html>
  <head>
    <script src="xhr.js"></script>
    <script>  
    
      if (!localStorage.stopwords) localStorage.stopwords = "";
      if (!localStorage.whitelist) localStorage.whitelist = "";
      
      // Called when the url of a tab changes.
      function checkForValidUrl(tabId, changeInfo, tab) {
        // is it google groups directory?
        if (tab.url.indexOf('groups.google.') != -1 
                && tab.url.indexOf('/topics') != -1 
               && tab.url.indexOf('/thread/') == -1) {
          // ... show the page action.
          chrome.pageAction.show(tabId);
          
          
          // handle requests
          chrome.extension.onRequest.addListener(
            function(request, sender, cb) {
              console.log("got request:");
              console.dir(request);
              console.dir(sender);
              console.dir(cb);
              if (request.spamCount) {
                chrome.pageAction.setPopup({"tabId": tabId, 
                    "popup": "popup.html#"+request.spamCount});
                cb({message : "Spam count received."});
                return;
              }
              if (request.getStorage) {
                if (!localStorage.master || !localStorage.lastUpdate || 
                     (new Date).getTime() - localStorage.lastUpdate > 1000 * 60 * 60) 
                  updateMasterList(function(){cb(localStorage);});
                else cb(localStorage);
                return;
              }
              cb({});
          });
          
        }
      };
      
      
    
    // Listen for any changes to the URL of any tab.
    chrome.tabs.onUpdated.addListener(checkForValidUrl);
      
      
    </script>
  </head>
</html>
